package com.zr.bookcorner.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zh.baselibrary.base.BaseNoIMActivity;
import com.zh.baselibrary.base.BasePresenter;
import com.zh.baselibrary.constant.BaseConstant;
import com.zh.baselibrary.constant.ExtraName;
import com.zh.baselibrary.utils.BarUtil;
import com.zh.baselibrary.utils.IntentUtils;
import com.zr.bookcorner.BookApplication;
import com.zr.bookcorner.R;


import butterknife.BindView;
import butterknife.OnClick;


public class LimitActivity extends BaseNoIMActivity {

    @BindView(R.id.tv_notice)
    TextView mTvNotice;
    @BindView(R.id.iv_limit)
    ImageView mIvLimit;
    @BindView(R.id.ll_back)
    RelativeLayout mLlBack;

    private Integer type;

    @Override
    protected int getLayoutId() {
        return R.layout.limit_activity;
    }

    @Override
    protected void init() {
        type = getIntent().getIntExtra(ExtraName.KEY_DATA, -1);
        if (type == -1) {
            finish();
        }
        if (type == 1) {
            //授权时间
            mTvNotice.setText("设备授权时间已超过，请联系工作人员");
            mIvLimit.setImageDrawable(getResources().getDrawable(R.mipmap.timeout));
            mLlBack.setBackgroundResource(BookApplication.isBig() ? R.mipmap.timeout_bg_big : R.mipmap.timeout_bg_mini);
        } else {
            //锁定
            mTvNotice.setText(" 设备已被锁定，无法使用");
            mLlBack.setBackgroundResource(R.drawable.limit_back);
            mIvLimit.setImageDrawable(getResources().getDrawable(R.mipmap.lock));
        }
        registerReceiver(mBroadcastReceiver, new IntentFilter(BaseConstant.BROAT_CAST_ACTION_UN_LOCK));
    }

    BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String typeStr = intent.getStringExtra(ExtraName.KEY_DATA);
            if (Integer.parseInt(typeStr) == type) {
                IntentUtils.instance().startActivityFinish(LimitActivity.this, HomeActivity.class);
            }
        }
    };
    private int count = 0;

    @OnClick({R.id.iv_1, R.id.iv_2, R.id.iv_3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_1:
                if (count == 0) {
                    count++;
                }
                break;
            case R.id.iv_2:
                if (count == 1) {
                    count++;
                }
                break;
            case R.id.iv_3:
                if (count == 2) {
                    count++;
                }
                if (count == 3) {
                    BarUtil.showBar();
                    finish();
                    Intent intent = new Intent();// 为Intent设置Action、Category属性
                    intent.setAction(Intent.ACTION_MAIN);// "android.intent.action.MAIN"
                    intent.addCategory(Intent.CATEGORY_HOME); //"android.intent.category.HOME"
                    startActivity(intent);

                } else {
                    count = 0;
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBroadcastReceiver != null) {
            unregisterReceiver(mBroadcastReceiver);
        }
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

}
